//
//  TYImpactEngine.m
//  TYUIKit
//
//  Created by TuyaInc on 2019/1/7.
//

#import "TYImpactEngine.h"

#import <UIKit/UIKit.h>

#import <objc/runtime.h>

typedef enum : NSUInteger {
    TYGeneratorTypeUnknow,
    TYGeneratorTypeImpactLight,
    TYGeneratorTypeImpactMedium,
    TYGeneratorTypeImpactHeavy,
    TYGeneratorTypeImpactSelection,
    TYGeneratorTypeImpactNotification,
} TYGeneratorType;

@interface TYImpactEngine ()

@property (nonatomic, strong) __kindof NSObject *currentGenerator;
@property (nonatomic) TYGeneratorType currentGeneratorType;

@end

@implementation TYImpactEngine

+ (instancetype)shareInstance {
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[[self class] alloc] init];
    });
    return instance;
}

- (instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}

+ (TYGeneratorType)_generatorTypeWityStyle:(TYImpactStyle)style {
    TYGeneratorType type;
    switch (style) {
        case TYImpactStyleLight:
            type = TYGeneratorTypeImpactLight;
            break;
        case TYImpactStyleMedium:
            type = TYGeneratorTypeImpactMedium;
            break;
        case TYImpactStyleHeavy:
            type = TYGeneratorTypeImpactHeavy;
            break;
        case TYImpactStyleSelection:
            type = TYGeneratorTypeImpactSelection;
            break;
        case TYImpactStyleNotificationSucc: case TYImpactStyleNotificationFail: case TYImpactStyleNotificationError:
            type = TYGeneratorTypeImpactNotification;
            break;
        default:
            type = TYGeneratorTypeUnknow;
            break;
    }
    return type;
}

#pragma mark - Public
+ (void)impactWithStyle:(TYImpactStyle)style {
    if (@available(iOS 10.0, *)) {
        TYGeneratorType type = [self _generatorTypeWityStyle:style];
        if (type == TYGeneratorTypeUnknow) {
            return;
        }
        
        UIFeedbackGenerator *generator;
        if ([TYImpactEngine shareInstance].currentGeneratorType == type) {
            generator = [TYImpactEngine shareInstance].currentGenerator;
        }
        if (!generator) {
            switch (type) {
                case TYGeneratorTypeImpactLight: case TYGeneratorTypeImpactMedium: case TYGeneratorTypeImpactHeavy:
                    generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:(UIImpactFeedbackStyle)style];
                    break;
                case TYGeneratorTypeImpactSelection:
                    generator = [[UISelectionFeedbackGenerator alloc] init];
                    break;
                case TYGeneratorTypeImpactNotification:
                    generator = [[UINotificationFeedbackGenerator alloc] init];
                default:
                    break;
            }
            if (!generator) {
                return;
            }
            
            [generator prepare];
            
            [TYImpactEngine shareInstance].currentGenerator = generator;
            [TYImpactEngine shareInstance].currentGeneratorType = type;
        }
        
        if ([generator isKindOfClass:[UIImpactFeedbackGenerator class]]) {
            UIImpactFeedbackGenerator *impact = (UIImpactFeedbackGenerator *)generator;
            [impact impactOccurred];
        } else if ([generator isKindOfClass:[UISelectionFeedbackGenerator class]]) {
            UISelectionFeedbackGenerator *select = (UISelectionFeedbackGenerator *)generator;
            [select selectionChanged];
        } else if ([generator isKindOfClass:[UINotificationFeedbackGenerator class]]) {
            UINotificationFeedbackGenerator *notif = (UINotificationFeedbackGenerator *)generator;
            [notif notificationOccurred:(UINotificationFeedbackType)(type - TYImpactStyleNotificationSucc + 1)];
        }
    }
}

@end
